//==================================================================================
//		Dual Quad Board Program
//		file name : Initial_Serial_Parallel_Interface.C
//		version : 1.0
//		target device : w78e516(winbond)
//		target board : dual quad board
//		c compiler : Keil
//==================================================================================

void Start_I2C_EEPROM();
void Stop_I2C_EEPROM();
void Ack_Check_EEPROM();
void I2C_1Byte_Write_EEPROM(U8);
U8 I2C_1Byte_Read_EEPROM();
void I2C_Data_Write(U8, U8, U8);
U8 I2C_Data_Read(U8, U8);

//void Chip_Sel_All_Off();
//void Chip_Sel_Page0();
//void Chip_Sel_Page1();
//void Chip_Sel_Page2();
//void Write_Asic_1Byte(U8, U8, U8);
//void Write_Asic_Table(U8, U8, U8 *, U8);
//U8	Read_Asic_1Byte(U8, U8);
//Function for TW2835 (070817)
void WriteAsicByteOSD(U8, U8, U8);                //070814
void WriteAsicByte(U8, U8, U8, U8);
void WriteAsicTable(U8, U8, U8, U8 *, U8);
U8	ReadAsicByte(U8, U8, U8);
U8	ReadAsicOSDMemWr(U8);//void);

//Copy from Cmn.c of TW2835 (070819)

U8	GetAsicFlgType(U8, U8, U8, U8);
void	SetAsicFlgType(U8, U8, U8, U8, U8);
//U8	GetAsicVar8Type(U8, U8, U8, U8);
//void	SetAsicVar8Type(U8, U8, U8, U8, U8);

void Start_I2C();
void Stop_I2C();
void Ack_Check();
void I2C_1Byte_Write(U8);
//U8 I2C_1Byte_Read();
void Write_I2C_1Byte(U8, U8, U8);
//U8 Read_I2C_1Byte(U8, U8);
//==================================================================================
//						DS1307 Serial function description
//
//
//
//
//==================================================================================
void Start_I2C()
{
	I2C_SDA = 1;
	I2C_SCL = 1;
	I2C_SDA = 0;
	I2C_SCL = 0;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Stop_I2C()
{
	I2C_SDA = 0;
	I2C_SCL = 1;
	I2C_SDA = 1;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Ack_Check()
{
	U8 t1;
	
	I2C_SDA = 1;	
	I2C_SCL = 1;
	for(t1=0;t1<3;t1++);
	I2C_SCL = 0;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void I2C_1Byte_Write(U8 wdata)
{
	U8 i;

	for(i=0;i<8;i++){
		(wdata&BIT7) ?  (I2C_SDA=1) : (I2C_SDA=0);
		I2C_SCL = 1;
		wdata<<=1;
		I2C_SCL = 0;
	}
		Ack_Check();
}
#if 0
//==================================================================================
//
//
//
//
//
//==================================================================================
U8 I2C_1Byte_Read()
{
	U8 rdata=0, i;

	I2C_SDA = 1;	
	for(i=0;i<7;i++){
		I2C_SCL = 1;
		if(I2C_SDA == 1)	rdata |= 0x01;
		I2C_SCL = 0;
		rdata<<=1;
	}
	
	I2C_SCL = 1;
	if(I2C_SDA == 1)	rdata |= 0x01;
	I2C_SCL = 0;
	
	Ack_Check();
	
	return rdata;
}
#endif
//==================================================================================
//						RTC: DS1307 IIC Bus control
//
//
//
//
//==================================================================================
void Write_I2C_1Byte(U8 slave_addr,U8 address,U8 wdata)
{
	Start_I2C();
	I2C_1Byte_Write(slave_addr);
	I2C_1Byte_Write(address);
	I2C_1Byte_Write(wdata);
	Stop_I2C();
}
#if 0
//==================================================================================
//
//
//
//
//
//==================================================================================
U8 Read_I2C_1Byte(U8 slave_addr,U8 address)
{
	U8 rdata=0;
	
	Start_I2C();
	I2C_1Byte_Write(slave_addr);
	I2C_1Byte_Write(address);
	Start_I2C();
	I2C_1Byte_Write(slave_addr|0x01);
	rdata=I2C_1Byte_Read();
	Stop_I2C();

	return rdata;
}
#endif
//==================================================================================
//----------------------E2PROM WRITE ROUTINE ----------------------------
//==================================================================================

void wait_for(int wait)
{
	int i = 0; 
	for(i = 0 ;i < wait; i++)
	{

	}
}

void Start_I2C_EEPROM()
{
	I2C_SDA_EEPROM = 1;
	I2C_SCL_EEPROM = 1;
	wait_for(200);
	I2C_SDA_EEPROM = 0;
	wait_for(200);
	I2C_SCL_EEPROM = 0;
	wait_for(200);
}
//==================================================================================
void Stop_I2C_EEPROM()
{
	I2C_SDA_EEPROM = 0;
	wait_for(200);
	I2C_SCL_EEPROM = 1;
	I2C_SDA_EEPROM = 1;
	wait_for(200);
}
//==================================================================================
void Ack_Check_EEPROM()
{
	U8 t1;
	
	I2C_SDA_EEPROM = 1;	
	I2C_SCL_EEPROM = 1;
	for(t1=0;t1<3;t1++);
	I2C_SCL_EEPROM = 0;
}
//==================================================================================
void I2C_1Byte_Write_EEPROM(U8 wdata)
{
	U8 i;

	for(i=0;i<8;i++){
		(wdata&BIT7) ?  (I2C_SDA_EEPROM=1) : (I2C_SDA_EEPROM=0);
		I2C_SCL_EEPROM = 1;
		wait_for(200);
		wdata<<=1;
		I2C_SCL_EEPROM = 0;
		wait_for(200);
	}
		Ack_Check_EEPROM();
}
//==================================================================================
U8 I2C_1Byte_Read_EEPROM()
{
	U8 rdata=0, i;

	I2C_SDA_EEPROM = 1;	
	for(i=0;i<7;i++){
		I2C_SCL_EEPROM = 1;
		wait_for(200);
		if(I2C_SDA_EEPROM == 1)	rdata |= 0x01;
		wait_for(200);
		I2C_SCL_EEPROM = 0;
		rdata<<=1;
		wait_for(200);
	}
	
	I2C_SCL_EEPROM = 1;
	wait_for(200);
	if(I2C_SDA_EEPROM == 1)	rdata |= 0x01;
	wait_for(200);
	I2C_SCL_EEPROM = 0;
	Ack_Check_EEPROM();
	wait_for(200);
	return rdata;
}
//==================================================================================
//==================================================================================
void I2C_Data_Write(U8 slaveid,U8 slaveadd,U8 wdata)
{
	U16 i;
	
	for(i=0;i<2000;i++);
	Start_I2C_EEPROM();
	I2C_1Byte_Write_EEPROM(slaveid);
	I2C_1Byte_Write_EEPROM(slaveadd);
	I2C_1Byte_Write_EEPROM(wdata);
	Stop_I2C_EEPROM();
	for(i=0;i<2000;i++);
}
//==================================================================================

//----------------------E2PROM READ ROUTINE ----------------------------
//==================================================================================
U8 I2C_Data_Read(U8 slaveid,U8 slaveadd)
{
	U8 rdata;

	Start_I2C_EEPROM();
	I2C_1Byte_Write_EEPROM(slaveid);
	I2C_1Byte_Write_EEPROM(slaveadd);
	Start_I2C_EEPROM();
	I2C_1Byte_Write_EEPROM((slaveid|0x01));
	rdata = I2C_1Byte_Read_EEPROM();
	Stop_I2C_EEPROM();
	
	return rdata;
}
//==================================================================================


//==================================================================================
//						Parallel function description
//==================================================================================

/*void Chip_Sel_All_Off()
{
	HCSB1 = 1;
	HCSB0 = 1;
}
//==================================================================================
void Chip_Sel_Page0()
{
	Chip_Sel_All_Off();

	HCSB0 = 0;		
	HCSB1 = 0;
}
//==================================================================================
void Chip_Sel_Page1()
{
	Chip_Sel_All_Off();

	HCSB0 = 1;
	HCSB1 = 0;
}
//==================================================================================
void Chip_Sel_Page2()
{
	Chip_Sel_All_Off();

	HCSB0 = 0;
	HCSB1 = 1;
}*/
//==================================================================================
/*void Write_Asic_1Byte(U8 page_cs, U8 wadd, U8 wdata)
{
//	EA = 0;
	
	HWRB = 1;
	HRDB = 1;
	HALE = 0;

	if(page_cs == PAGE0)			Chip_Sel_Page0();
	else if(page_cs == PAGE1)	Chip_Sel_Page1();
	else if(page_cs == PAGE2)	Chip_Sel_Page2();

	MDATA =	wadd;
	HALE = 1;
	HALE = 0;
	MDATA =	wdata;
	HWRB = 0;
	HWRB = 1;
	Chip_Sel_All_Off();
	
	MDATA =	0xff;

//	EA = 1;
}*/
//==================================================================================
/*void Write_Asic_Table(U8 page_cs, U8 wadd, U8 *tbl_ptr, U8 tbl_cntr)
{
	U8 i;
	
//	EA = 0;
	
	HWRB = 1;
	HRDB = 1;
	HALE = 0;

	if(page_cs == PAGE0)			Chip_Sel_Page0();
	else if(page_cs == PAGE1)	Chip_Sel_Page1();
	else if(page_cs == PAGE2)	Chip_Sel_Page2();

	MDATA =	wadd;
	HALE = 1;
	HALE = 0;

	for(i=0;i<tbl_cntr;i++)
	{
		MDATA =	*tbl_ptr;
		HWRB = 0;
		HWRB = 1;
		tbl_ptr++;
	}	
	Chip_Sel_All_Off();

	MDATA =	0xff;

//	EA = 1;
}*/
//==================================================================================
/*U8 Read_Asic_1Byte(U8 page_cs, U8 radd)
{
	U8 read_data;
	
//	EA = 1;
	
	HWRB = 1;
	HRDB = 1;
	HALE = 0;

	if(page_cs == PAGE0)	       	Chip_Sel_Page0();
	else if(page_cs == PAGE1)	Chip_Sel_Page1();
	else if(page_cs == PAGE2)	Chip_Sel_Page2();

	MDATA =	radd;
	HALE = 1;
	HALE = 0;
	MDATA =	0xff;
	HRDB = 0;
	read_data =	MDATA;
	HRDB = 1;
	Chip_Sel_All_Off();

//	EA = 1;
	
	return read_data;
}*/
//==================================================================================
//Copy for TW2835 osd function (070817)
//
//
//
//
//==================================================================================
void WriteAsicByteOSD(U8 _dvc, U8 _addr, U8 _wdat)
{
//#if	defined(__EN_ISR__)
//		EA = 0;
//#endif
		 _dvc = _dvc;
		HCSB0= 1;
        HWRB = 1;                                 //The following 3 defines are epual above 'P1=0xf6'   (07-08-09) 
		HRDB = 1;
		HALE = 0;
//#if	1 //defined(__4CH__)
		//_dvc = 0;			//... temporal
		HCSB0=0;
		HCSB1=1;
/*
#elif	defined(__16CH__)
		P1 &= 0xf7;//(_dvc^0xff)|0x90;
#endif
*/
		MDATA =	_addr;
		HALE = 1;
		HALE = 0;
		MDATA =	_wdat;
		HWRB = 0;
		HWRB = 1;

//#if	defined(__4CH__)
		HCSB0=1;
		HCSB1=1;
/*
#elif	defined(__16CH__)
		P1 |= 0xd8;
#endif
*/
		MDATA =	0xff;

//#if	defined(__EN_ISR__)
//		EA = 1;
//#endif
//	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void WriteAsicByte(U8 _dvc, U8 _pg, U8 _addr, U8 _wdat)
{
//#if	defined(__EN_ISR__)
//		EA = 0;
//#endif

              HWRB = 1;                                 //The following 3 defines are epual above 'P1=0xf6'   (07-08-09) 
		HRDB = 1;
		HALE = 0;
#if	defined(__4CH__)
		_dvc = 0;			//... temporal
		if(_pg == DVC_PG0)			Chip_Sel_Page0();
		else if(_pg == DVC_PG1)		Chip_Sel_Page1();
		else if(_pg== DVC_PG2)		Chip_Sel_Page2();
		
#elif	defined(__16CH__)
		if(_pg == DVC_PG0)	P1 = _dvc^0xff;//P2 = _dvc^0xff;
		else if(_pg == DVC_PG1)	P1 = (_dvc^0xff)|0x48;//P2 = (_dvc^0xff)|0x55;
		else if(_pg == DVC_PG2)	P1 = (_dvc^0xff)|0x90;//P2 = (_dvc^0xff)|0xaa;

#endif

		MDATA =	_addr;
		HALE = 1;
		HALE = 0;
		MDATA =	_wdat;
		HWRB = 0;
		HWRB = 1;

#if	defined(__4CH__)
		Chip_Sel_All_Off();
#elif	defined(__16CH__)
		P1 = (_dvc^0xff)|0xd8;
#endif

		MDATA =	0xff;

//#if	defined(__EN_ISR__)
//		EA = 1;
//#endif
//	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void WriteAsicTable(U8 _dvc, U8 _pg, U8 _addr, U8 *_tbl_ptr, U8 _tbl_cnt)
{
	U8 _t1_;

//#if	defined(__EN_ISR__)
//		EA = 0;
//#endif

              HWRB = 1;                                 //The following 3 defines are epual above 'P1=0xf6'   (07-08-09) 
		HRDB = 1;
		HALE = 0;
#if	defined(__4CH__)
		_dvc = 0;			//... temporal
		if(_pg == DVC_PG0)			Chip_Sel_Page0();
		else if(_pg == DVC_PG1)		Chip_Sel_Page1();
		else if(_pg== DVC_PG2)		Chip_Sel_Page2();
#elif	defined(__16CH__)
		if(_pg == DVC_PG0)	P1 = _dvc^0xff;//P1 &= _dvc;//P2 = _dvc^0xff;
		else if(_pg == DVC_PG1)	P1 = (_dvc^0xff)|0x48;//P1 = (P1 & _dvc)|0x48;//P2 = (_dvc^0xff)|0x55;
		else if(_pg == DVC_PG2)	P1 = (_dvc^0xff)|0x90;//P1 = (P1 & _dvc)|0x90;//P2 = (_dvc^0xff)|0xaa;
#endif

		MDATA =	_addr;
		HALE = 1;
		HALE = 0;

		for(_t1_=0;_t1_<_tbl_cnt;_t1_++)
		{
			MDATA =	*_tbl_ptr;
			HWRB = 0;
			HWRB = 1;
			_tbl_ptr++;
		}	

#if	defined(__4CH__)
		HCSB0=1;                              //be equal 'Chip_Sel_All_Off()'    (07-08-09)
		HCSB1=1;
		
#elif	defined(__16CH__)
		P1 = (_dvc^0xff)|0xd8;
#endif

		MDATA =	0xff;

//#if	defined(__EN_ISR__)
//		EA = 1;
//#endif
}

//==================================================================================
//
//
//
//
//
//==================================================================================
U8 ReadAsicByte(U8 _dvc, U8 _pg, U8 _addr)
{
	U8 _rdat_;

//#if	defined(__EN_ISR__)
//		EA = 0;
//#endif

              HWRB = 1;                                 //The following 3 defines are epual above 'P1=0xf6'   (07-08-09) 
		HRDB = 1;
		HALE = 0;
#if	defined(__4CH__)
		_dvc = 0;			//... temporal
		if(_pg == DVC_PG0)			Chip_Sel_Page0();
		else if(_pg == DVC_PG1)		Chip_Sel_Page1();
		else if(_pg== DVC_PG2)		Chip_Sel_Page2();
#elif	defined(__16CH__)
		if(_pg == DVC_PG0)	       P1 =  _dvc^0xff;//P2 = _dvc^0xff;
		else if(_pg == DVC_PG1)	P1 = (_dvc^0xff)|0x48;//P2 = (_dvc^0xff)|0x55;
		else if(_pg == DVC_PG2)	P1 = (_dvc^0xff)|0x90;//P2 = (_dvc^0xff)|0xaa;
#endif

		MDATA =	_addr;
		HALE = 1;
		HALE = 0;
		MDATA =	0xff;		// port0(MDATA) input mode
		HRDB = 0;
		_rdat_ =	MDATA;
//DLY_FOR(30);
		HRDB = 1;

#if	defined(__4CH__)
		HCSB0=1;                              //be equal 'Chip_Sel_All_Off()'    (07-08-09)
		HCSB1=1;
		
#elif	defined(__16CH__)
		P1 = (_dvc^0xff)|0xd8;
#endif

//#if	defined(__EN_ISR__)
//		EA = 1;
//#endif
	return _rdat_;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
//*
U8	ReadAsicOSDMemWr(U8 _dvc)//void)
{
	U8 _rdat_;

//	if(_pg == DVC_PG0){
//		_rdat_ = ReadI2CByte(0x28,_addr);
//	}
//	else{
#if	defined(__EN_ISR__)
		EA = 0;
#endif

		//P1 = 0xf6;					//... initial state
                                                  		//... P[7:4]:INT[5:2], P1_3:HALE, P1_2:HRDB, P1_1:HWRB, P1_0:HSPB
              HWRB = 1;                                 //The following 3 defines are epual above 'P1=0xf6'   (07-08-09) 
		HRDB = 1;
		HALE = 0;

#if	defined(__4CH__)
		_dvc = 0;			//... temporal
		HCSB0=0;
		HCSB1=1;
#elif	defined(__16CH__)
		P1 = (_dvc^0xff)|0x90;//P2 = (_dvc^0xff)|0xaa;
#endif

		MDATA =	0x0a;
		HALE = 1;
		HALE = 0;
		MDATA =	0xff;		// port0(MDATA) input mode
		HRDB = 0;
		_rdat_ =	MDATA;
		HRDB = 1;


//		P1 = 0xf6;					//... chip select all off
		//P1 = 0xf6;					//... initial state
//		P2 = 0x03;					//... chip select all off
#if	defined(__4CH__)
		HCSB0=1;
		HCSB1=1;
#elif	defined(__16CH__)
		P1 |= 0xd8;
#endif


#if	defined(__EN_ISR__)
		EA = 1;
#endif
//	}
	return _rdat_;
}

//==================================================================================
//Copy from 	Cmn.c of TW2835 (070819)
//==================================================================================
/*
void	SetCrntDvcCh(U8 _ch, U8 *_crnt_dvc, U8 *_crnt_ch)
{
#if defined(__4CH__)
	*_crnt_dvc = MASTER;
	*_crnt_ch = _ch;
#elif	defined(__16CH__)
	*_crnt_dvc = MASTER<<((_ch&0x0c)>>1);
	*_crnt_ch = _ch&0x03;
#endif
}*/
	
//==================================================================================
//
//
//
//
//
//==================================================================================
U8	GetAsicFlgType(U8 _dvc, U8 _pg, U8 _addr, U8 _flg)
{
	U8 _t1_=0;

	_t1_ = ReadAsicByte(_dvc,_pg,_addr);

//	return	(_t1_&_flg) ? (ON):(OFF);			//... only 0 or 1
	return	_t1_&_flg;								//... status of destination bit
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void	SetAsicFlgType(U8 _dvc, U8 _pg, U8 _addr, U8 _flg, U8 _data)
{
	U8 _t1_;

	_t1_ = ReadAsicByte(_dvc,_pg,_addr);
	_t1_ = (_t1_ & ~_flg)|_data;
	WriteAsicByte(_dvc,_pg,_addr,_t1_);
}
//==================================================================================
/*
U8	GetAsicVar8Type(U8 _dvc, U8 _pg, U8 _addr, U8 _flg)
{
	U8 _ofst_, _wdth_;
	U8 _t1_;

	_ofst_ = (_flg&0xf0)>>4;
	_wdth_ = _flg&0x0f;

	_flg = 0;
	for(_t1_=0;_t1_<_wdth_;_t1_++)	_flg += BIT0<<_t1_;
//	if(_wdth_ == 1)	_wdth_ = 0x01;
//	else if(_wdth_ == 2)	_wdth_ = 0x03;	
//	else if(_wdth_ == 3)	_wdth_ = 0x07;
//	else if(_wdth_ == 4)	_wdth_ = 0x0f;
//	else if(_wdth_ == 5)	_wdth_ = 0x1f;
//	else if(_wdth_ == 6)	_wdth_ = 0x3f;
//	else if(_wdth_ == 7)	_wdth_ = 0x7f;
//	else if(_wdth_ == 8)	_wdth_ = 0xff;

	_flg <<= _ofst_;
	_t1_ = ReadAsicByte(_dvc,_pg,_addr)&_flg;

	return	_t1_>>_ofst_;										//... value of destination bit
//	return	_t1_&_flg;							//... status of destination bit
//	return	(_t1_&_flg) ? (ON):(OFF);		//... only 0 or 1
}
//==================================================================================
void	SetAsicVar8Type(U8 _dvc, U8 _pg, U8 _addr, U8 _flg, U8 _data)
{
	U8 _ofst_, _wdth_;
	U8 _t1_;

	_ofst_ = (_flg&0xf0)>>4;
	_wdth_ = _flg&0x0f;

	_flg = 0;
	for(_t1_=0;_t1_<_wdth_;_t1_++)	_flg += BIT0<<_t1_;
//	if(_wdth_ == 1)	_wdth_ = 0x01;
//	else if(_wdth_ == 2)	_wdth_ = 0x03;	
//	else if(_wdth_ == 3)	_wdth_ = 0x07;
//	else if(_wdth_ == 4)	_wdth_ = 0x0f;
//	else if(_wdth_ == 5)	_wdth_ = 0x1f;
//	else if(_wdth_ == 6)	_wdth_ = 0x3f;
//	else if(_wdth_ == 7)	_wdth_ = 0x7f;
//	else if(_wdth_ == 8)	_wdth_ = 0xff;

	_flg <<= _ofst_;
	_t1_ = ReadAsicByte(_dvc,_pg,_addr);
	_t1_ = (_t1_ & ~_flg)|(_data<<_ofst_);
	WriteAsicByte(_dvc,_pg,_addr,_t1_);
}
//	*/
//==================================================================================
